using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
using Ideopuzzle.Resources;


namespace Ideopuzzle
{
    public class MainMenu : Menu
    {
        #region Fields
        
        SpriteFont font36, font72, font14b;
        AudioManager audio_manager;
        TextObject trial;

        #endregion

        public MainMenu(Game game)
            : base(game)
        {
            
        }

        public override void Initialize()
        {
            Game.Components.Add(new InputManager(Game));
            input_manager = Game.Services.GetService(typeof(IInputManager)) as IInputManager;
            input_manager.OnTap += new InputManager.Tap(OnTap);

            base.Initialize();
        }

        protected override void LoadContent()
        {
            sprite_batch = Game.Services.GetService(typeof(SpriteBatch)) as SpriteBatch;
            audio_manager = Game.Services.GetService(typeof(AudioManager)) as AudioManager;
            font36 = Game.Content.Load<SpriteFont>("Fonts/Light36");
            font72 = Game.Content.Load<SpriteFont>("Fonts/Light72");
            font14b = Game.Content.Load<SpriteFont>("Fonts/Bold14");

            trial = new TextObject(sprite_batch, font14b, Lang.Trial, 40, Color.Red);

            CreateButtonsList();
            LoadRenderableObjects();

            base.LoadContent();
        }

        protected override void Dispose(bool disposing)
        {
            if(input_manager!=null)
                input_manager.OnPress -= new InputManager.Tap(OnTap);
            Game.Services.RemoveService(typeof(IInputManager));

            base.Dispose(disposing);
        }

        public override void Update(GameTime gameTime)
        {
            if (GamePad.GetState(PlayerIndex.One).IsButtonDown(Buttons.Back))
            {
                if (GIdeopuzzle.IsTrial) 
                    ScreenManager.ExitGame(Game);
                else
                    Game.Exit();

                audio_manager.PlayClick();
            }
            base.Update(gameTime);
        }

        private float trial_alpha;
        public override void Draw(GameTime gameTime)
        {
            GraphicsDevice.Clear(Color.Black);

            base.Draw(gameTime);

            if (GIdeopuzzle.IsTrial)
            {
                trial_alpha = (1.0f + ((float)Math.Sin(gameTime.TotalGameTime.TotalSeconds * 3.0f))) / 2.0f;

                trial.Draw(trial_alpha);
            }
        }

        #region Private Methods

        private void CreateButtonsList()
        {
            int offset = 50;
            int start_y = 250;
            string[] labels = new string[] { Lang.MM_Play, Lang.MM_Options, Lang.MM_Credits, Lang.MM_Exit };
            

            entries = new List<MenuEntry>();

            for (int i = 0; i < labels.Length; i++)
            {
                float x = GraphicsDevice.Viewport.Width / 2.0f - font36.MeasureString(labels[i]).X / 2.0f;

                MenuEntry e = new LabelButton(sprite_batch, font36, new Vector2(x, start_y + offset * i), Color.White, Color.Black, labels[i], 0);
                e.OnPress +=new Press(GetButtonEventHandler(i));

                entries.Add(e);   
            }       
        }

        private void LoadRenderableObjects()
        {
            renderables = new List<RenderableObject>();

            renderables.Add(new ImgObject(sprite_batch, Game.Content.Load<Texture2D>("Textures/MainMenu/menubackground"), 
                Vector2.Zero));
            
            renderables.Add(new TextObject(sprite_batch, font72, Lang.MM_Title, 40));

            var tm = font72.MeasureString(Lang.MM_Title);
            renderables.Add(new ImgObject(sprite_batch, Game.Content.Load<Texture2D>("Textures/MainMenu/title_underline"), 
                new Vector2(GraphicsDevice.Viewport.Width / 2.0f - tm.X / 2.0f, (int)(15 + tm.Y)), Color.White, (int)tm.X, 
                Game.Content.Load<Texture2D>("Textures/MainMenu/title_underline").Height));

            renderables.Add(new ImgObject(sprite_batch, Game.Content.Load<Texture2D>("Textures/Shared/tiles"),
                new Vector2(GraphicsDevice.Viewport.Width / 2.0f - Game.Content.Load<Texture2D>("Textures/Shared/tiles").Width / 2.0f, 25 + tm.Y)));
        }

        private Action<MenuEntry> GetButtonEventHandler(int index)
        {
            switch (index)
            {
                case 0:
                    return (MenuEntry m) => { ScreenManager.StartLevel(Game); audio_manager.PlayClick(); };
                case 1:
                    return (MenuEntry m) => { ScreenManager.SelectOptions(Game); audio_manager.PlayClick(); };
                case 2:
                    return (MenuEntry m) => { ScreenManager.SelectCredits(Game); audio_manager.PlayClick(); };
                default:
                    return (MenuEntry m) => { if (GIdeopuzzle.IsTrial) ScreenManager.ExitGame(Game); else Game.Exit(); audio_manager.PlayClick(); };
            }
        }

        #endregion
    }
}
